<template>
  <el-container class="server-container">
    <el-aside width="250px">
      <!-- openvidu选择框 -->
      <el-row>
        <el-select v-model="openviduSelect" @change="serversData" placeholder="请选择OpenVidu服务" class="aside-select">
          <el-option
            v-for="item in openviduInfo"
            :key="item.hostname"
            :value="item.hostname">
          </el-option>
        </el-select>
      </el-row>
      <!-- 会话搜索框 -->
      <el-row>
        <el-input v-model="searchData" @change="searchServer" suffix-icon="el-icon-search" placeholder="请输入会议名称" class="aside-search"/>
        <el-button icon="el-icon-refresh" @click="serversData" plain/>
      </el-row>
      <!--  会话列表    -->
      <el-row v-for="item in sessionShowList" :key="item.session">
        <el-button style="margin: 5px 5px;width:95%" @click="selectSession(item)" v-bind:type="(chooseSession.sessionId === item.sessionId)?'success':'default'">{{item.sessionId}}</el-button>
      </el-row>
    </el-aside>
    <el-container>
      <el-main>
        <el-row>
          <el-divider content-position="left">当前会议ID: {{chooseSession.sessionId}}</el-divider>
        </el-row>
        <el-row>
          <el-table
            :data="tableData"
            height="750"
            stripe
            style="width: 100%"
          >
            <el-table-column
              fixed
              prop="event"
              label="事件类型"
              width="200"
              align="center"
            />
            <el-table-column
              prop="sessionId"
              label="触发事件会话的唯一标识符"
              width="200"
              align="center"
            />
            <el-table-column
              prop="timestampStr"
              label="触发事件的时间戳（毫秒）"
              width="200"
              align="center"
            />
            <el-table-column
              prop="startTimeStr"
              label="事件的开始时间"
              width="200"
              align="center"
            />
            <el-table-column
              prop="duration"
              label="事件的持续时间"
              width="200"
              align="center"
            />
            <el-table-column
              prop="reason"
              label="事件产生原因"
              width="200"
              align="center"
            />
            <el-table-column
              prop="participantId"
              label="参与者的唯一标识"
              width="200"
              align="center"
            />
<!--            <el-table-column-->
<!--              prop="location"-->
<!--              label="地理位置"-->
<!--            />-->
            <el-table-column
              prop="platform"
              label="平台"
              width="200"
              align="center"
            />
            <el-table-column
              prop="connection"
              label="媒体连接类型"
              width="200"
              align="center"
            />
            <el-table-column
              prop="receivingFrom"
              label="媒体流的来源的唯一标识"
              width="200"
              align="center"
            />
            <el-table-column
              prop="audioEnabled"
              label="开启音频"
            />
            <el-table-column
              prop="videoEnabled"
              label="开启视频"
            />
            <el-table-column
              prop="videoSource"
              label="视频的类型"
            />
            <el-table-column
              prop="videoFramerate"
              label="视频的帧率"
            />
            <el-table-column
              prop="videoDimensions"
              label="视频的分辨率"
              width="200"
              align="center"
            />
            <el-table-column
              prop="id"
              label="录制对象的唯一标识"
              width="200"
              align="center"
            />
            <el-table-column
              prop="name"
              label="录制文件的名称"
              width="200"
              align="center"
            />
            <el-table-column
              prop="outputMode"
              label="录制的输出模式"
            />
            <el-table-column
              prop="hasAudio"
              label="录制是否有音频"
            />
            <el-table-column
              prop="hasVideo"
              label="录制是否有视频"
            />
            <el-table-column
              prop="recordingLayout"
              label="录制布局类型"
            />
            <el-table-column
              prop="resolution"
              label="录制文件的分辨率"
            />
            <el-table-column
              prop="size"
              label="视频文件的大小（字节）"
            />
            <el-table-column
              prop="status"
              label="录制状态"
            />
          </el-table>
        </el-row>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { ovinfoAll } from '@/api/openviduInfo'
import { openviduDetail } from '@/api/server'
import { queryCdrRecords } from '@/api/cdr'

export default {

  data() {
    return {
      preSelect:null,
      openviduSelect: null,
      openviduInfo: [],
      sessionSourceList: [],
      searchData: null,
      sessionShowList: [],
      chooseSession: {},
      fullscreenLoading: false,
      tableData: []
    }
  },
  mounted: function() {
    this.preSelect = this.$route.params.hostname
    console.log(this.preSelect)
    this.selectList()
  },
  methods: {
    selectList() {
      return new Promise((resolve, reject) => {
        ovinfoAll({}).then(response => {
          const { success, data, message } = response
          if (success) {
            this.openviduInfo = data
            for (let item of this.openviduInfo) {
              console.log(item)
              console.log(item.hostname)
              if (this.preSelect !== null && this.preSelect === item.hostname) {
                this.openviduSelect = this.preSelect
                this.serversData()
                this.preSelect = null
                break
              }
            }
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    serversData() {
      if (this.openviduSelect === undefined || this.openviduSelect === null) {
        return
      }
      return new Promise((resolve, reject) => {
        openviduDetail({
          hostname: this.openviduSelect
        }).then(response => {
          const { success, data, message } = response
          if (success) {
            this.sessionSourceList = data.activeSessions
            this.searchServer()
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    searchServer() {
      console.log('this.searchData:' + this.searchData)
      if (this.searchData === undefined || this.searchData === null) {
        this.sessionShowList = this.sessionSourceList
        return
      }
      const search = this.searchData
      this.sessionShowList = this.sessionSourceList.filter(function(item, index) {
        return item.sessionId.indexOf(search) !== -1
      });
    },
    selectSession(session) {
      // 表单刷新
      this.chooseSession = session
      this.getTableData(session)
    },
    getTableData(session) {
      if (this.openviduSelect === undefined || this.openviduSelect === null) {
        return
      }
      if (session === undefined || session === null) {
        return
      }
      return new Promise((resolve, reject) => {
        queryCdrRecords({
          hostname: this.openviduSelect,
          sessionId: session.sessionId
        }).then(response => {
          const { success, data, message } = response
          if (success) {
            this.tableData = data
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    loading() {
      this.fullscreenLoading = true
      setTimeout(() => {
        this.fullscreenLoading = false
      }, 1000)
    }
  }
}
</script>

<style type="scss">
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
  }

  .el-main {
    background-color: #D3DCE6;
    color: #333;
    padding: 5px 5px;
  }

  .server-container {
    background-color: #D3DCE6;
    height: 1250px;
  }

  .badge-item {
    margin-right: 18px;
  }

  .box-card {
    margin-left: 5px;
    margin-top: 5px;
    width: 240px;
  }

  .aside-select {
    margin-left: 5px;
    margin-top: 5px;
    width: 240px;
  }

  .aside-refresh {
    width: 40px;
  }

  .aside-search {
    margin-left: 5px;
    margin-top: 5px;
    width: 180px;
  }

  .con-table-expand {
    font-size: 0;
  }
  .con-table-expand label {
    width: 60px;
    color: #99a9bf;
  }
  .con-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 15%;
  }

  .choose-card {
    background-color: goldenrod;
  }
</style>

